package com.kingyun.gpsinspection.purificationservice.services.tdm.innerservice.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.kingyun.gpsinspection.purificationservice.common.enums.TaskTypeEnum;
import com.kingyun.gpsinspection.purificationservice.dao.mapper.tdm.StatisticalManageTasksMapper;
import com.kingyun.gpsinspection.purificationservice.dao.mapper.tdm.TdmWorkOrderMapper;
import com.kingyun.gpsinspection.purificationservice.facade.model.jsm.EmployeeVo;
import com.kingyun.gpsinspection.purificationservice.facade.model.tdm.*;
import com.kingyun.gpsinspection.purificationservice.facade.service.tdm.StatisticalManageTasksService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 任务统计管理service
 * Created by jiangjx on 2017/2/4.
 */
public class StatisticalManageTasksServiceImpl implements StatisticalManageTasksService {

    @Autowired
    private StatisticalManageTasksMapper statisticalManageTasksMapper;

    @Autowired
    private TdmWorkOrderMapper tdmWorkOrderMapper;

    /**
     * 加载table数据
     *
     * @param smtVo 查询条件封装类
     * @return
     */
    @Override
    public JSONObject queryBarInfo(SMTVo smtVo) {
        JSONObject jsonObject = new JSONObject();
        /*根据各种情况补充默认条件*/
        List<SMT> baseRows = statisticalManageTasksMapper.loadPieInfo(smtVo);//查询数据
        jsonObject.put("jsonData", formatData(baseRows));//返回结果
        return jsonObject;
    }

    /**
     * 加载详细表格数据
     *
     * @param smtVo 查询条件封装类
     * @return
     */
    @Override
    public JSONObject loadDetailTable(SMTVo smtVo) {
        JSONObject jsonObject = new JSONObject();
        /*装载查询条件*/
        smtVo.setLimit(null == smtVo.getLimit() ? 10 : smtVo.getLimit());
        smtVo.setOffset(null == smtVo.getOffset() ? 0 : smtVo.getOffset());
        /* 进行分页查询分离 */
        jsonObject.put("total", statisticalManageTasksMapper.queryDetailCount(smtVo));//查询总条数
        List<SMTForTaskInfo> baseRows = statisticalManageTasksMapper.queryDetailTableList(smtVo);//查询分页内容
        /*返回结果*/
        jsonObject.put("rows", JSON.toJSON(baseRows));
        return jsonObject;
    }

    /**
     * 查看工单详细
     *
     * @param workOrderId 工单主ID
     * @return
     */
    @Override
    public WorkOrderVo getWorkOrderInfo(String workOrderId) {
        return tdmWorkOrderMapper.queryByPrimaryKey(workOrderId);
    }

    /**
     * 加载饼图数据
     *
     * @param smtVo 查询条件封装类
     */
    @Override
    public JSONArray loadPieInfo(SMTVo smtVo) {
        JSONArray array = new JSONArray();
        //查询数据
        List<SMT> baseRows = statisticalManageTasksMapper.queryTableList(smtVo);
        //循环统计已完成，总任务个数。
        Map<String, Integer> map = new HashMap<>();
        for (SMT smt : baseRows) {
            map.put("已完成", map.containsKey("已完成") ? map.get("已完成") + smt.getCompleted() : smt.getCompleted());
            map.put("未完成", map.containsKey("未完成") ? map.get("未完成") + (smt.getTaskSum() - smt.getCompleted()) : smt.getTaskSum() - smt.getCompleted());
        }
        //封装数据
        for (Object key : map.keySet()) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("name", key);
            jsonObject.put("value", map.get(key));
            array.add(jsonObject);
        }
        return array;
    }

    /**
     * 获取父组织id
     *
     * @param orgId 当前组织id
     */
    @Override
    public JSONObject queryParentOrgId(String orgId) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("orgId", statisticalManageTasksMapper.queryParentOrgId(orgId));
        return jsonObject;
    }

    /**
     * 格式化统计数据
     *
     * @param baseRows 原始统计数据
     */
    private JSONObject formatData(List<SMT> baseRows) {
        JSONObject jsonObject = new JSONObject();
        Map<String, Short> returnMap = new HashMap<>();
        //循环统计子部门的10大任务的总数
        for (SMT smt : baseRows) {
            //统计巡回检查
            returnMap.put(TaskTypeEnum.TASK_TYPE_ENUM_1.getDecription(), returnMap.containsKey(TaskTypeEnum.TASK_TYPE_ENUM_1.getDecription()) ? (short) (returnMap.get(TaskTypeEnum.TASK_TYPE_ENUM_1.getDecription()) + smt.getTask1()) : smt.getTask1());
            //统计维护保养
            returnMap.put(TaskTypeEnum.TASK_TYPE_ENUM_3.getDecription(), returnMap.containsKey(TaskTypeEnum.TASK_TYPE_ENUM_3.getDecription()) ? (short) (returnMap.get(TaskTypeEnum.TASK_TYPE_ENUM_3.getDecription()) + smt.getTask3()) : smt.getTask3());
            //统计常规操作
            returnMap.put(TaskTypeEnum.TASK_TYPE_ENUM_2.getDecription(), returnMap.containsKey(TaskTypeEnum.TASK_TYPE_ENUM_2.getDecription()) ? (short) (returnMap.get(TaskTypeEnum.TASK_TYPE_ENUM_2.getDecription()) + smt.getTask2()) : smt.getTask2());
            //统计分析处理
            returnMap.put(TaskTypeEnum.TASK_TYPE_ENUM_5.getDecription(), returnMap.containsKey(TaskTypeEnum.TASK_TYPE_ENUM_5.getDecription()) ? (short) (returnMap.get(TaskTypeEnum.TASK_TYPE_ENUM_5.getDecription()) + smt.getTask5()) : smt.getTask5());
            //统计日常事务
            returnMap.put(TaskTypeEnum.TASK_TYPE_ENUM_11.getDecription(), returnMap.containsKey(TaskTypeEnum.TASK_TYPE_ENUM_11.getDecription()) ? (short) (returnMap.get(TaskTypeEnum.TASK_TYPE_ENUM_11.getDecription()) + smt.getTask11()) : smt.getTask11());
        }
        //封装X轴任务名数组
        JSONArray name = new JSONArray();
        for (TaskTypeEnum taskTypeEnum : TaskTypeEnum.values()) {
            name.add(taskTypeEnum.getDecription());
        }

        //封装Y轴数值数组
        JSONArray value = new JSONArray();
        for (String key : returnMap.keySet()) {
            JSONObject object = new JSONObject();
            object.put("name", key);
            object.put("value", returnMap.get(key));
            value.add(object);
        }
        //put数据
        jsonObject.put("x", name);
        jsonObject.put("y", value);
        return jsonObject;//返回值
    }

    /**
     * 查询任务列表
     *
     * @param employeeVo
     * @param task       查询条件封装类
     * @return
     */
    @Override
    public Map<String, Object> loadTaskTable(EmployeeVo employeeVo, Task task) {
        Map<String, Object> map = new HashMap<>();
        task.setLimit(task.getLimit() == null ? 10 : task.getLimit());
        task.setOffset(task.getOffset() == null ? 0 : task.getOffset());
        //分页查询记录
        List<Task> list = statisticalManageTasksMapper.queryList(task);
        map.put("rows", list);
        map.put("total", statisticalManageTasksMapper.queryCount(task));
        return map;
    }
}
